<template>
  <div class="padding-l-20 padding-r-20 padding-t-20 h-100 table-page">
    <filter-bar>
      <template slot="left-bar">
        <a-space>
          <a-input
            class="position-r"
            placeholder="请输入关键字搜索"
            v-model="searchForm.word"
          />
          <a-button class="nbcs-btn-search" @click="page.currentPage = 1;getList(page, searchForm)">查询</a-button>
          <a-button class="nbcs-btn-default" @click="clearObj(searchForm)">重置</a-button>
        </a-space>
      </template>
    </filter-bar>
    <interval-bg></interval-bg>
    <opt-bar>
      <div slot="left-bar">
        白名单词库
      </div>
      <div slot="right-bar">
        <a-button @click="edit(0)" class="nbcs-btn-main-bg" style="top: -1.6px" v-if="permission('sys:whiteWord:add')">新增</a-button>
      </div>
    </opt-bar>
    <a-table ref="_table" bordered :columns="columns" :data-source="list" :pagination="pageConfig" rowKey="id" :loading="loading" :scroll="{ x: 1000, y: tableHeight }">
      <template slot="roles" slot-scope="text">
        <a-tag v-for="(item, index) in text" :key="index">{{ item.name }}</a-tag>
      </template>
      <span slot="tags" slot-scope="text, record">
        <a-tag v-if="record.enabled" class="tag-green"> 正常 </a-tag>
        <a-tag v-else class="tag-red"> 锁定 </a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <a-space size="middle">
          <a @click="edit(2, record)">查看</a>
          <a @click="edit(1, record)" v-if="permission('sys:whiteWord:update')">编辑</a>
          <a-popconfirm title="确定删除该词汇吗？" @confirm="del(record)" v-if="permission('sys:whiteWord:delete')">
            <a-icon slot="icon" type="question-circle-o" style="color: red" />
            <a class="table-red-btn">删除</a>
          </a-popconfirm>
        </a-space>
      </span>
    </a-table>
    <white-words-edit ref="whiteWordsEdit" @getList="getList(page, searchForm)"></white-words-edit>
  </div>
</template>

<script>
import FilterBar from '@/layouts/FilterBar.vue';
import IntervalBg from "@/layouts/IntervalBg";
import OptBar from "@/layouts/OptBar";
import { list, delwhite } from '@/services/word';
import whiteWordsEdit from './whiteWordsEdit.vue';
import { cutDateSecond } from '@/utils/date';
import {clearObj} from '../../utils/util';
export default {
  name: 'WordList',
  components: { FilterBar, whiteWordsEdit, IntervalBg, OptBar},
  data() {
    return {
      tableHeight: 0,
      loading: false,
      searchForm: {
        word: '',
      },
      list: [],
      columns: [
        { title: '白名单词汇', dataIndex: 'word' },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          customRender: (text) => {
            return cutDateSecond(text);
          },
        },
        {
          title: "创建账号",
          dataIndex: "createUserName",
        },
        {
          title: '操作',
          align: 'center',
          key: 'operation',
          fixed: 'right',
          width: 180,
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageConfig: {
        showQuickJumper: true,
        showSizeChanger: true,
        hideOnSinglePage: false, //只有一页时是否隐藏分页器
        defaultPageSize: 20,
        pageSizeOptions: ['20', '30', '50'],
        total: 0,
        current: 1,
        showTotal: (total) => {
          return `共 ${total} 条`;
        },
        onShowSizeChange: (page, pageSize) => {
          this.page.currentPage = 1;
          this.page.pageSize = pageSize;
          this.getList(this.page, this.searchForm);
        },
        onChange: (page) => {
          this.page.currentPage = page;
          this.pageConfig.current = page;
          this.getList(this.page, this.searchForm);
        },
      },
      page: {
        currentPage: 1,
        pageSize: 20,
      },
    };
  },
  computed: {},
  created() {
    this.getList(this.page);
  },
  mounted() {
      this.tableHeight = document.querySelector('.table-page').clientHeight - 276;
  },
  methods: {
    permission(name) {
      let permissions = this.$store.getters["account/permissions"];
      return permissions.filter(m => m.id == name || m.operation.indexOf(name) >= 0).length > 0;
    },
    getList(page, params) {
      this.loading = true;
      list(
        Object.assign(
          {
            page: page.currentPage,
            pageSize: page.pageSize,
            sensitiveType: 'WHITE',
          },
          params
        )
      ).then((res) => {
        this.loading = false;
        const { data } = res.data;
        this.list = data.list;
        this.pageConfig.current = data.page;
        this.pageConfig.total = data.totalRow;
      });
    },

    del(obj) {
      delwhite({ id: obj.id }).then((res) => {
        if (res.data.code === 0) {
          this.$message.success('删除成功');
          this.getList(this.page);
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //添加/编辑
    edit(action, data = {}) {
      this.$refs['whiteWordsEdit'].changeForm(true, action, data);
    },
    clearObj
  },
};
</script>

<style lang="less" scoped>
.select {
  width: 170px;
  /deep/ .ant-select-selection {
    width: 100%;
  }
}
._input {
  width: 180px;
}
.table-page {
  .ant-table-wrapper {
    height: calc(100% - 150px);
  }
}
</style>
